﻿using System;
using System.Collections.Generic;
namespace CSharp.Basic.Learn.发布订阅
{
    public abstract class FishingTool
    {
        private readonly List<ISubscriber> _subscribers;

        protected FishingTool()
        {
            _subscribers = new List<ISubscriber>();
        }

        /// <summary>
        /// 添加观察者
        /// </summary>
        /// <param name="subscriber">Subscriber.</param>
        public void Attach(ISubscriber subscriber)
        {
            if (!_subscribers.Contains(subscriber))
                _subscribers.Add(subscriber);
        }

        public void Remove(ISubscriber subscriber)
        {
            if (_subscribers.Contains(subscriber))
                _subscribers.Remove(subscriber);
        }


        public void Notify(object args)
        {
            foreach (var subscriber in _subscribers)
                subscriber.Receive(args);
        }
    }
}
